﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace Wys.DataForm.CSharp
{
    public partial class CSharChildMenu : Wys.Control.Vb.AutoAdjustFrom
    {
        public CSharChildMenu()
        {
            InitializeComponent();
        }

        #region 选择父窗体 
        private void btnSelect_Click(object sender, EventArgs e)
        {
            CSharpMenu cs = new CSharpMenu();
            if (cs.ShowDialog() == DialogResult.OK)
            {
                TreeNode tn = cs.GetSelectNode();
                if (tn != null)
                {
                    this.txtParentName.Text = tn.Text;
                    this.txtParentTitle.Text = tn.Tag.ToString();
                }
            }         
        }

        #endregion

        #region 禁用窗体的部分功能
        private void radChild_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if (rb.Name == "radChild")
            {
                
                this.groupBox5.Enabled = true;
                this.groupBox2.Enabled = false;
                this.tabControl1.SelectedIndex = 0;
                this.groupBox6.Enabled = true ;
            }
            else if (rb.Name == "radNoChild")
            {
                this.groupBox5.Enabled = false;
                this.groupBox2.Enabled = true;
                this.tabControl1.SelectedIndex = 1;
                this.groupBox6.Enabled = false;
                this.radTopMenu.Checked = false;
                this.radNotTopMenu.Checked = true;
            }
            else if (rb.Name == "radTopMenu")
            {              
                this.groupBox5.Enabled = true;
                this.groupBox2.Enabled = false;
                //this.groupBox4.Enabled = false;
                this.btnSelect.Enabled = false;
                this.tabControl1.SelectedIndex = 0;
                this.groupBox1.Enabled = false;
            }
            else if (rb.Name == "radNotTopMenu")
            {
                if (this.radChild.Checked)
                {
                    this.groupBox5.Enabled = true;
                    this.groupBox2.Enabled = false;
                }
                else if (this.radNoChild.Checked)
                {
                    this.groupBox5.Enabled = false;
                    this.groupBox2.Enabled = true;
                }
                this.btnSelect.Enabled = true;
                this.groupBox2.Enabled = true;
                //this.groupBox4.Enabled = true;
                this.groupBox1.Enabled = true;
                
            }

        }
        #endregion        

        #region 窗体初始化时间
        private void CSharChildMenu_Load(object sender, EventArgs e)
        {
            txtParentName.Tag = "";
            this.radChild.Checked = true;
            this.groupBox5.Enabled = true;
            this.groupBox2.Enabled = false;
            //加载图像
            LoadMenuImage();
        }
        #endregion

        #region 插入按钮
        private void myButton1_Click(object sender, EventArgs e)
        {
            String strSql = String.Empty;
            ADODB.Recordset rs = null;

            #region 如果是顶级菜单不否则判断
            if (radNotTopMenu.Checked == true)
            {
                if (this.txtParentTitle.Text == String.Empty)
                {
                    ShowMessageBox("必须选择父菜单");
                    this.btnSelect.Focus();
                    return;
                }             
            }
            #endregion            
     
            #region 判断父菜单的比入力项
            if (this.radChild.Checked)
            {
                if (this.txtName.Text == String.Empty)
                {
                    ShowMessageBox("名称不能为空");
                    this.txtName.Focus();
                    return;
                }
                if (this.txtTitle.Text == String.Empty)
                {
                    ShowMessageBox("标题不能为空为空");
                    this.txtTitle.Focus();
                    return;
                }              
            }
            #endregion           

            #region 判断子菜单的必入力项
            //如果是子菜单
            if (this.radNoChild.Checked)
            {
                if (this.txtChildTitle.Text == String.Empty)
                {
                    ShowMessageBox("标题不能为空");
                    this.txtChildTitle.Focus();
                    return;
                }
                if (this.txtChildName.Text == String.Empty)
                {
                    ShowMessageBox("类名不能为空");
                    this.txtChildName.Focus();
                    return;
                }
                if (this.txtChildDllName.Text == String.Empty)
                {
                    ShowMessageBox("组件名不能为空");
                    this.txtChildDllName.Focus();
                    return;
                }              
            }
            #endregion                        

            #region 验证子菜单 类名是否存在 访问键 快捷键
            if (this.radNoChild.Checked)
            {
                strSql = "select * from XSCMenuBackUp where CLASSNAME='" + txtChildName.Text + "'";
                //rs = rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                rs = ct.GetTable(strSql);
                if (rs != null)
                {
                    if (rs.EOF == false&&btnParam.Enabled==false )
                    {
                        ShowMessageBox("菜单已经存在:" + rs.Fields["TITLE"].Value.ToString());
                        rs.Close();
                        this.txtName.Focus();
                        return;
                    }
                    else
                    {
                        rs.Close();
                    }
                }

                //判断访问键是否唯一
                //if (this.cboShortCuts.Text != string.Empty)
                //{
                //    strSql = "select * from XSCMenuBackUp where ShortCuts='" + cboShortCuts.Text.Trim() + "' AND ParentMenu='" + txtParentTitle.Text + "'";
                //    //rs = rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                //    rs = ct.GetTable(strSql);
                //    if (rs != null)
                //    {
                //        if (rs.EOF == false)
                //        {
                //            ShowMessageBox("访问键已经存在:" + rs.Fields["TITLE"].Value.ToString());
                //            cboShortCuts.Focus();
                //            rs.Close();
                //            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //            return;
                //        }
                //        else
                //        {
                //            rs.Close();
                //            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //        }
                //    }
                //    else
                //    {
                //        rs.Close();
                //        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //    }
                //}
                //判断快捷键是否唯一
                //if (cboKeys.Enabled == true)
                //{
                //    if (cboKeys.Text == string.Empty)
                //    {
                //        ShowMessageBox("快捷键不能为空");
                //        cboKeys.Focus();
                //        return;
                //    }
                //    else
                //    {
                //        strSql = "select * from XSCMenuBackUp where 1=1 ";
                //        strSql += " AND  Keys='" + cboKeys.Text + "' ";
                //        if (chkCtrl.Checked)
                //        {
                //            strSql += " AND  CtrlKey=1 ";
                //        }
                //        else
                //        {
                //            strSql += " AND  CtrlKey=0 ";
                //        }

                //        if (chkShift.Checked)
                //        {
                //            strSql += " AND  ShiftKey=1 ";
                //        }
                //        else
                //        {
                //            strSql += " AND  ShiftKey=0 ";
                //        }
                //        if (chkAlt.Checked)
                //        {
                //            strSql += " AND  AltKey=1 ";
                //        }
                //        else
                //        {
                //            strSql += " AND  AltKey=0 ";
                //        }

                //        //rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                //        rs = ct.GetTable(strSql);
                //        if (rs != null)
                //        {
                //            if (rs.EOF == false)
                //            {
                //                ShowMessageBox("快捷键键已经存在:" + rs.Fields["TITLE"].Value.ToString());
                //                cboShortCuts.Focus();
                //                rs.Close();
                //                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //                return;
                //            }
                //            else
                //            {
                //                rs.Close();
                //                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //            }
                //        }

                //    }
                }
            //}
            #endregion

            #region 验证父菜单 名称是否存在 访问键
            //父菜单
            if (this.radChild.Checked)
            {

                strSql = "select * from XSCMenuBackUp where MENUNAME='" + txtName.Text + "'";
                //rs = rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                rs = ct.GetTable(strSql);
                if (rs != null)
                {
                    if (rs.EOF == false)
                    {
                        ShowMessageBox("菜单已经存在:" + rs.Fields["TITLE"].Value.ToString());
                        rs.Close();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                        this.txtName.Focus();
                        return;
                    }
                    else
                    {
                        rs.Close();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                    }
                }

                //判断访问键是否唯一
                //if (this.cboParentShortKeys.Text != string.Empty)
                //{
                    //如果是顶级子菜单
                //    if (radTopMenu.Checked == false)
                //    {
                //        strSql = "select * from XSCMenuBackUp where ShortCuts='" + cboParentShortKeys.Text.Trim() + "' AND ParentMenu='" + txtParentTitle.Text + "'";

                //    }
                //    else
                //    {
                //        strSql = "select * from XSCMenuBackUp where ShortCuts='" + cboParentShortKeys.Text.Trim() + "' AND ParentMenu is null ";
                //    }

                //    //rs = rs = ReflectFunction(ct, "GetTable", new Object[] { strSql }) as ADODB.Recordset;
                //    rs = ct.GetTable(strSql);
                //    if (rs != null)
                //    {
                //        if (rs.EOF == false)
                //        {
                //            ShowMessageBox("访问键已经存在:" + rs.Fields["TITLE"].Value.ToString());
                //            cboParentShortKeys.Focus();
                //            rs.Close();
                //            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //            return;
                //        }
                //        else
                //        {
                //            rs.Close();
                //            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //        }
                //    }
                //    else
                //    {
                //        rs.Close();
                //        System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                //    }
                //}
            }
            #endregion           

            #region 开始插入数据库
            strSql = "select  * from XSCMenuBackUp";
            //rs = rs = ReflectFunction(ct, "GetUpdateRs", new Object[] { strSql }) as ADODB.Recordset;
            rs = ct.GetUpdateRs(strSql);
            if (rs != null)
            {
                rs.AddNew(System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                //如果是顶层窗口父菜单为NULL
                if (radTopMenu.Checked)
                {

                }
                else
                {
                    rs.Fields["ParentMenu"].Value = this.txtParentTitle.Text;
                }
                //如果菜单类型为父菜单
                if (radChild.Checked)
                {
                    rs.Fields["MENUNAME"].Value = this.txtName.Text;
                    rs.Fields["TITLE"].Value = this.txtTitle.Text;
                    rs.Fields["IsParentMenu"].Value = 1;
                    //更新访问键
                    if (this.cboParentShortKeys.Text != string.Empty)
                    {
                        rs.Fields["ShortCuts"].Value = cboParentShortKeys.Text;
                    }
                }
                else if (radNoChild.Checked)
                {
                    rs.Fields["CLASSNAME"].Value = this.txtChildName.Text;
                    rs.Fields["TITLE"].Value = this.txtChildTitle.Text;
                    rs.Fields["IsParentMenu"].Value = 0;
                    rs.Fields["DLLDIRECTORY"].Value = "DLLDIRECTORY";
                    rs.Fields["TYPE"].Value = iType;
                    rs.Fields["DLLTYPE"].Value = 0;
                    rs.Fields["DLLNAME"].Value = txtChildDllName.Text;

                    if (rad附加显示.Checked)
                    {
                        rs.Fields["IsAttach"].Value = 1;
                    }
                    else
                    {
                        rs.Fields["IsAttach"].Value = 0;
                    }

                    if (chk安全访问.Checked)
                    {
                        rs.Fields["RequirePassword"].Value = 1;
                    }
                    else
                    {
                        rs.Fields["RequirePassword"].Value = 0;
                    }

                    //判断是否为外部组件 2010-05-15
                    if (chk外部组件.Checked)
                    {
                        if (rad外部EXE.Checked)
                        {
                            rs.Fields["IsImportDll"].Value = 1;
                        }
                        else if (rad外部Dll.Checked)
                        {
                            rs.Fields["IsImportDll"].Value = 2;
                        }
                        else
                        {
                            rs.Fields["IsImportDll"].Value = 3;
                        }
                    }
                    else
                    {
                        rs.Fields["IsImportDll"].Value = 0;
                    }
                    //判断是否为外部组件 2010-05-15

                    //更新访问键
                    if (this.cboShortCuts.Text != string.Empty)
                    {
                        rs.Fields["ShortCuts"].Value = cboShortCuts.Text;
                    }

                    if (cboKeys.Enabled = true && cboKeys.Text != "")
                    {
                        rs.Fields["keys"].Value = this.cboKeys.Text;
                        if (chkCtrl.Checked)
                        {
                            rs.Fields["CtrlKey"].Value = 1;
                        }
                        else
                        {
                            rs.Fields["CtrlKey"].Value = 0;
                        }
                        if (chkAlt.Checked)
                        {
                            rs.Fields["AltKey"].Value = 1;
                        }
                        else
                        {
                            rs.Fields["AltKey"].Value = 0;
                        }
                        if (chkShift.Checked)
                        {
                            rs.Fields["ShiftKey"].Value = 1;
                        }
                        else
                        {
                            rs.Fields["ShiftKey"].Value = 0;
                        }
                    }
                    //2010-04-24
                    //是否使用参数
                    bool bParam = false;
                    foreach (String str in alValue)
                    {
                        if (str != "")
                        {
                            bParam = true;
                            break;
                        }
                    }
                    if (bParam)
                    {
                        rs.Fields["IsParam"].Value = 1;
                        string strParam = "";
                        for (int i = 0; i < alParam.Count; i++)
                        {
                            strParam += alParam[i] + ":" + alValue[i] + ";";
                        }
                        rs.Fields["ParamValue"].Value = strParam;
                    }
                    else
                    {
                        rs.Fields["IsParam"].Value = 0;
                    }
                    //2010-04-24
                }
                //2009-07-18 -A
                if (!String.IsNullOrEmpty(this.cboMenuFile.Text))
                {
                    rs.Fields["ImagePath"].Value = this.cboMenuFile.Text;
                }
                //2009-07-18 -A
                rs.Fields["OptUser"].Value = this.strName;
                rs.Fields["USERNAME"].Value = this.strName;
                rs.Fields["AddTime"].Value = DateTime.Now.ToString();
                rs.Fields["OPTIME"].Value = DateTime.Now.ToString();
                rs.Update(System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                rs.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
                ShowMessageBox("更新成功");
            }
            #endregion          
            
        }
        #endregion

        #region 选择反射窗体
        private void myButton3_Click(object sender, EventArgs e)
        {
            ChildSelect cs = new ChildSelect();
            if (cs.ShowDialog() == DialogResult.OK)
            {
                this.txtChildName.Text = cs.ClassName1;
                this.txtChildTitle.Text = cs.Title;
                this.txtChildDllName.Text = cs.DllName;
                if (!cs.是否外部)
                {
                    SetFormParam(cs.DllName, cs.ClassName1);
                }
            }
        }
        #endregion

        #region 控制快捷键
        private void chkCtrl_CheckStateChanged(object sender, EventArgs e)
        {
            if ((chkAlt.Checked ==false) && (chkCtrl.Checked == false) && (chkShift.Checked == false))
            {
                cboKeys.Enabled = false;
            }
            else
            {
                cboKeys.Enabled = true ;
            }
        }
        #endregion

        #region 关闭按钮事件
        private void myButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion    

        #region 加载菜单图像
        public void LoadMenuImage()
        {
            //cboMenuFile.Items.Clear();
            //String strFolder = System.Configuration.ConfigurationManager.AppSettings["MenuFileFolder"];
            //System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(strFolder);
            //if (di != null)
            //{
            //    foreach (System.IO.FileInfo fi in di.GetFiles("*.ico"))
            //    {
            //        this.cboMenuFile.Items.Add(fi.Name);
            //    }
            //}
        }
        #endregion        

        private void myButton4_Click(object sender, EventArgs e)
        {
            MenuIcoManager mmc = new MenuIcoManager();
            if (mmc.ShowDialog() == DialogResult.OK)
            {
                if (!String.IsNullOrEmpty(mmc.IconName))
                {
                    cboMenuFile.Text = mmc.IconName;
                    p1.Image = this.cDllRelease.GetImageFormResourceName(cboMenuFile.Text);
                }
            }           
        }

        #region 重置按钮 A-2009-09-05
        private void btnUpdateForm_Click(object sender, EventArgs e)
        {
            UpdateMenu frm = new UpdateMenu();
            frm.iType = this.iType;
            frm.strName = this.strName;
            frm.ShowDialog();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {

        }
        #endregion              

        #region 设置参数 2010-04-24
        System.Collections.ArrayList alParam = new System.Collections.ArrayList();
        System.Collections.ArrayList alValue = new System.Collections.ArrayList();
        private void SetFormParam(String strDll, String ClassName)
        {
            try
            {
                btnParam.Enabled = false;
                alParam.Clear();
                alValue.Clear();
                String strPath = Path.Combine(this.GetCurrentDirectory(), strDll);
                System.Reflection.Assembly tempas = System.Reflection.Assembly.LoadFile(strPath);
                System.Type ty = tempas.GetType(ClassName);
                PropertyInfo[] pis = ty.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    if (pi.Name.StartsWith("选项参数_"))
                    {
                        alParam.Add(pi.Name);
                        alValue.Add("");
                    }
                }
                if (alParam.Count > 0)
                {
                    btnParam.Enabled = true;
                }
            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
            }
            
        }
        #endregion

        #region 设置参数 2010-04-24
        /// <summary>
        /// 查询参数
        /// </summary>
        /// <param name="strDll">组件的全名</param>
        /// <param name="ClassName">类的名称</param>
        /// <param name="strParamVlaue">数据库的参数值</param>
        private void SetFormParam(String strDll, String ClassName, String strParamVlaue)
        {
            btnParam.Enabled = false;
            alParam.Clear();
            alValue.Clear();
            String strPath = Path.Combine(this.GetCurrentDirectory(), strDll);
            System.Reflection.Assembly tempas = System.Reflection.Assembly.LoadFile(strPath);
            System.Type ty = tempas.GetType(ClassName);
            PropertyInfo[] pis = ty.GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                if (pi.Name.StartsWith("选项参数_"))
                {
                    alParam.Add(pi.Name);
                    alValue.Add("");
                }
            }
            //参数值格式
            //类型:1;标题:英语报表
            if (strParamVlaue != null)
            {
                foreach (String str in strParamVlaue.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    if (str != "")
                    {
                        String[] value = str.Split(":".ToCharArray());
                        if (value.Length == 2)
                        {
                            int index = alParam.IndexOf(value[0]);
                            if (index >= 0)
                            {
                                alValue[index] = value[1];
                            }
                        }
                    }
                }
            }
            if (alParam.Count > 0)
            {
                btnParam.Enabled = true;
            }
        }
        #endregion

        #region 设置窗体的参数 2010-04-24
        private void btnParam_Click(object sender, EventArgs e)
        {
            using (FormParam fp = new FormParam())
            {
                for (int i = 0; i < alParam.Count; i++)
                {
                    if (i == 0)
                    {
                        fp.lbl1.Text = alParam[0].ToString();
                        if (alValue.Count > 0)
                        {
                            fp.txt1.Text = alValue[0].ToString();
                        }
                        fp.txt1.Enabled = true;
                    }
                    else if (i == 1)
                    {
                        fp.lbl2.Text = alParam[1].ToString();
                        if (alValue.Count > 1)
                        {
                            fp.txt2.Text = alValue[1].ToString();
                        }
                        fp.txt2.Enabled = true;
                    }
                    else if (i == 2)
                    {
                        fp.lbl3.Text = alParam[2].ToString();
                        if (alValue.Count > 2)
                        {
                            fp.txt3.Text = alValue[2].ToString();
                        }
                        fp.txt3.Enabled = true;
                    }
                    else if (i == 3)
                    {
                        fp.lbl4.Text = alParam[3].ToString();
                        if (alValue.Count > 3)
                        {
                            fp.txt4.Text = alValue[3].ToString();
                        }
                        fp.txt4.Enabled = true;
                    }
                    else if (i == 4)
                    {
                        fp.lbl5.Text = alParam[4].ToString();
                        if (alValue.Count > 4)
                        {
                            fp.txt5.Text = alValue[4].ToString();
                        }
                        fp.txt5.Enabled = true;
                    }
                }
                if (fp.ShowDialog() == DialogResult.OK)
                {
                    if (fp.txt1.Enabled)
                    {
                        alValue[0] = fp.txt1.Text.TrimEnd();
                    }
                    if (fp.txt2.Enabled)
                    {
                        alValue[1] = fp.txt2.Text.TrimEnd();
                    }
                    if (fp.txt3.Enabled)
                    {
                        alValue[2] = fp.txt3.Text.TrimEnd();
                    }
                    if (fp.txt4.Enabled)
                    {
                        alValue[3] = fp.txt4.Text.TrimEnd();
                    }
                    if (fp.txt5.Enabled)
                    {
                        alValue[4] = fp.txt5.Text.TrimEnd();
                    }
                }
            }
        }

        #endregion

        #region 追加从外部调用 2010-05-15
        private void chk外部组件_CheckedChanged(object sender, EventArgs e)
        {
            if (chk外部组件.Checked)
            {
                rad外部EXE.Enabled = true;
                rad外部Dll.Enabled = true;
            }
            else
            {
                rad外部EXE.Enabled = false;
                rad外部Dll.Enabled = false;
            }
        }
        #endregion 

    }
}
